package com.example.ljw.frescowallpaper.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.bumptech.glide.Glide;
import com.example.ljw.frescowallpaper.R;
import com.example.ljw.frescowallpaper.adapter.MyPagerAdapter;
import com.example.ljw.frescowallpaper.config.Constants;
import com.example.ljw.frescowallpaper.fragment.WallFragment;
import com.example.ljw.frescowallpaper.utils.GlideCircleTransform;
import com.example.ljw.frescowallpaper.utils.PreUtils;

import java.util.ArrayList;
import java.util.List;



public class NavigationActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private Context mContext = this;
    //抽屉用户名TextView
    private TextView userName_textView_drawer;
    //抽屉用户头像
    private ImageView imageView_nav_header;
    //tablayout初始化
    private TabLayout tabLayout_main;
    //初始化Viewpager
    private ViewPager viewPager_main;
    private String userName;
    private String userIcon;
    private List<Fragment> mTotalList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        initView();
        String objetc = PreUtils.readStrting(NavigationActivity.this,Constants.KEY_OF_OBJECTID);
        Toast.makeText(mContext,objetc , Toast.LENGTH_SHORT).show();



    }

    @Override
    protected void onResume() {
        super.onResume();
        //获取用户名
        userName = PreUtils.readStrting(NavigationActivity.this,Constants.KEY_HAVA_USER);
        //若获得的传值为用户名，则更改textview，若为注册/登录,则设置点击监听
        if (userName == ""){
            userName_textView_drawer.setText("注册/登录");
        }else {
            userName_textView_drawer.setText(userName);
            userName_textView_drawer.setClickable(false);
        }
        //获取头像
        userIcon = PreUtils.readStrting(NavigationActivity.this,Constants.KEY_WITH_ICON);
        Glide.with(mContext)
                .load(userIcon)
                    .transform(new GlideCircleTransform(mContext,2,mContext.getResources().getColor(R.color.orange)))
                .into(imageView_nav_header);

    }

    private void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);



        //侧滑菜单headerlayout的点击监听
        //动态将侧滑菜单中的头菜单加入布局
        View drawHeadView = navigationView.inflateHeaderView(R.layout.nav_header_navigation);
        //实例化注册登陆按钮
        userName_textView_drawer = (TextView) drawHeadView.findViewById(R.id.textView_nav_header);
//        TextView textView_nav_header = (TextView) drawHeadView.findViewById(R.id.textView_nav_header);
        imageView_nav_header = (ImageView) drawHeadView.findViewById(R.id.imageView_nav_header);

//        if (userName.equals("注册/登录")){
            //设置注册/登录按钮监听
            userName_textView_drawer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳转到登录界面
                    Intent intent = new Intent(mContext,LoginActivity.class);
                    startActivityForResult(intent, Constants.ASK_FOR_USER_REQUEST);
                }
            });
//        }



        // 隐藏状态栏，全屏显示
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setFlags(flag, flag);
        //初始化Viewpaper
        viewPager_main= (ViewPager) findViewById(R.id.viewPager_main);
        //从array资源中获取栏目信息
        String [] arrTabs=getResources().getStringArray(R.array.arrTabs);
        for (int i=0;i<arrTabs.length;i++){
            WallFragment wallFragment= WallFragment.newInstance(i+1);
            mTotalList.add(wallFragment);
        }
        //为viewpager设置适配器
        MyPagerAdapter adapter=new MyPagerAdapter(getSupportFragmentManager(),mTotalList,arrTabs);
        viewPager_main.setAdapter(adapter);
        //初始化tablayout
        tabLayout_main= (TabLayout) findViewById(R.id.tabLayout_main);
        tabLayout_main.setTabMode(TabLayout.MODE_FIXED);
        //设置tablayout位置在viewpaper上面
        tabLayout_main.setupWithViewPager(viewPager_main);



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.ASK_FOR_USER_REQUEST&&resultCode == Constants.ASK_FOR_USER_RESULT){
            String username = data.getStringExtra("username");
            String usericon = data.getStringExtra("usericon");
            Glide.with(mContext)
                    .load(usericon)
//                    .transform(new GlideCircleTransform(mContext,2,mContext.getResources().getColor(R.color.orange)))
                    .into(imageView_nav_header);
            userName_textView_drawer.setText(username);
        }
    }

    //点击返回键实现关闭抽屉
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.navigation, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_mine) {
            Intent intent = new Intent();
            intent.setClass(mContext,UserCenterActivity.class);
            intent.putExtra("caonima",userName);
            intent.putExtra("usericon",userIcon);
            startActivity(intent);
            // Handle the camera action
        } else if (id == R.id.nav_download) {
            Intent intent = new Intent(mContext,MyDownloadActivity.class);
            startActivity(intent);
        } else if (id == R.id.nav_favorite) {
            //获取objectid
            String objectId = PreUtils.readStrting(NavigationActivity.this, Constants.KEY_OF_OBJECTID);
            if (objectId.equals("8BkXsLLs")){
                Toast.makeText(mContext, "请登录后使用", Toast.LENGTH_SHORT).show();
            }else {
                Intent intent = new Intent(mContext,MyFavoriteActivity.class);
                startActivity(intent);
            }


        } else if (id == R.id.nav_myphoto) {
            //读取本地图片
            Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivity(intent);
        }
        else if (id == R.id.nav_setting) {
            Intent intent = new Intent(mContext,SettingActivity.class);
            startActivity(intent);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
